<script setup lang="ts">
import { ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { loginByPassword, loginByMobile } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useSendMobileCode } from '@/composables'
// 手机号
const mobile = ref('13230000001')
// 密码
const password = ref('abc12345')
// 控制同意选择框
const agree = ref(false)
// 控制短信登录和密码登录
const isPass = ref(false)
// 短信验证码
const code = ref('')

// 控制密码显示和隐藏开关
const isShow = ref(true)

// 本地仓库
const store = useUserStore()
// 路由实例对象
const router = useRouter()
// 路由信息对象
const route = useRoute()
// 登录
const submit = async () => {
  // 判断是否有没有勾选我已同意
  if (!agree.value) return showToast('请勾选同意')
  // 进行登录
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  // console.log(res)
  // 把返回的用户信息存储到仓库和本地
  store.setUser(res.data)
  // 提示登录成功
  showSuccessToast('登录成功')
  //登录回退 如果有route.query.rerurnUrl历史记录登陆以后就返回之前页 没有就直接跳转到user
  router.replace((route.query.returnUrl as string) || '/user')
}

// // 设置倒计时
// const time = ref(0)
// // form
// const form = ref<FormInstance>()
// //发送短信验证码
// const onSend = async () => {
//   //验证:倒计时 手机号
//   if (time.value > 0) return
//   // 检验手机号  name=mobile
//   await form.value?.validate('mobile')
//   // 校验成功发送验证码
//   const res: any = await sendMobileCode(mobile.value, 'login')
//   // 开始倒计时
//   time.value = 60
//   setTimeout(() => {
//     code.value = res.data.code
//   }, 5000)
//   showToast('发送成功')
//   let timer: any
//   //开始倒计时
//   if (timer) clearInterval(timer) //如果已经有定时器了就清理定时器
//   timer = setInterval(() => {
//     time.value--
//     // 倒计时结束清理定时器
//     if (time.value <= 0) clearInterval(timer)
//   }, 1000)
// }

const { onSend, form, time } = useSendMobileCode(mobile, 'login')
</script>

<template>
  <div class="login-page">
    <!-- 导航栏 -->
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '验证码登录' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{
          isPass ? '短信验证码登录' : '密码登录'
        }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="submit" ref="form">
      <van-field
        name="mobile"
        :rules="mobileRules"
        v-model="mobile"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        v-if="isPass"
        v-model="password"
        :rules="passwordRules"
        placeholder="请输入密码"
        :type="`${isShow ? 'text' : 'password'}`"
      >
        <template #button>
          <cp-icon
            @click="isShow = !isShow"
            :name="`login-eye-${isShow ? 'on' : 'off'}`"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        :rules="codeRules"
        v-else
        v-model="code"
        placeholder="请输入验证码"
      >
        <template #button>
          <span
            class="btn-send"
            :class="{ active: time > 0 }"
            @click="onSend"
            >{{ time > 0 ? `${time}再次发送` : '发送短信验证码' }}</span
          >
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round native-type="submit" type="primary"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a
        class="icon"
        href="https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
